/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */

import confenum from './confenum.js'

import proto from './proto.js'

import pubmud from './pubmud.js'

var data = {
  onpc: true,

  wsuri: 'ws://192.168.0.97:8800/Lunar',
  wssocket: '',

  pagename: 'game',
  modfuncname: 'test1',
  modinfoname: 'test1',

  pagemainheight: '300px',

  plogin: false,
  pbase: {},
  pmisc: {},
  pgame: {},
  pprop: [],

  mudtexts: [],
  muddebugs: [],

  end: ''
}

function devicecheck () {
  data.onpc = navigator.userAgent.toLowerCase().match(/(ipod|ipad|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|wince)/i) == null
  if (data.onpc) {
    jxdebug('devicecheck PC版本')
  } else {
    jxdebug('devicecheck 移动版本')
  }
}

const mudtdebugmax = 100
function jxdebug (text) {
  console.log(text)
  if (data.muddebugs.length > mudtdebugmax) {
    while (data.muddebugs.length > mudtdebugmax / 2) {
      data.muddebugs.shift()
    }
  }
  let list = [{
    text: text,
    color: 'white'
  }]
  data.muddebugs.push(list)
}

const mudtextmax = 100

function jxwdtext (txt) {
  jxwdtextc(txt, 'green')
}

function jxwdtextc (txt, color) {
  jxwdmsg({
    text: txt,
    color: color
  })
}

function jxwdtextmud (txt) {
  if (txt.indexOf('#') === -1) {
    jxwdtext(txt)
  } else {
    jxwdmsgs(pubmud.mudcolortext(txt))
  }
}

function jxwdmsg (obj) {
  jxwdmsgs([obj])
}

function jxwdmsgs (list) {
  if (data.mudtexts.length > mudtextmax) {
    while (data.mudtexts.length > mudtextmax / 2) {
      data.mudtexts.shift()
    }
  }
  data.mudtexts.push(list)
}

function jxpage (obj) {
  data.pagename = obj
}
function jxmodfunc (obj) {
  data.modfuncname = obj
  $('#modfuncdiv').modal('show')
}
function jxmodinfo (obj) {
  data.modinfoname = obj
  $('#modinfodiv').modal('show')
}

function wsreq (pid) {
  return {
    _protocol: proto.MapProtocolId[pid]
  }
}

function wssend (req) {
  data.wssocket.send(JSON.stringify(req))
}

function setinfoplayer (ibase, imisc, igame, iprop) {
  data.pbase = ibase
  data.pmisc = imisc
  data.pgame = igame
  data.pprop = iprop
}
function setinfoplayerbase (ibase, imisc, iprop) {
  data.pbase = ibase
  data.pmisc = imisc
  data.pprop = iprop
}

function getprop (name) {
  return data.pprop[confenum.confemProp[name]]
}

function isself (puid) {
  return (data.pbase.Puid === puid)
}

export default {
  data: data,

  setinfoplayer: setinfoplayer,
  setinfoplayerbase: setinfoplayerbase,
  getprop: getprop,
  isself: isself,

  jxdebug,

  jxwdtext,
  jxwdtextc,
  jxwdtextmud,
  jxwdmsg,
  jxwdmsgs,

  jxpage: jxpage,
  jxmodfunc: jxmodfunc,
  jxmodinfo: jxmodinfo,

  wsreq: wsreq,
  wssend: wssend,

  devicecheck: devicecheck
}
